﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace backoffice
{
    public partial class mainForm : Form
    {
        public mainForm()
        {
            InitializeComponent();
        }

        private void mainForm_Load(object sender, EventArgs e)
        {
            panelProducts.Visible = false;
            panelOrders.Visible = false;
            PopulateAndBindCategories();
            BU.ProductManager pm = new BU.ProductManager();
        }

        /**
         * 
         * Remplit le DataGridView des catégories
         * 
         **/
        private void PopulateAndBindCategories()
        {
            BU.CategoryManager cm = new BU.CategoryManager();
            //BU.Category category = (BU.Category)cm.getOne(1);

            //if (category == null)
            //{
            //    MessageBox.Show("The category id was not found, please check.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            //    this.Close();
            //    return;
            //}

            List<BU.Category> list = cm.getAll();

            dgvCategories.DataSource = list;
        }

        /**
         * 
         * Bouton d'édition d'une catégorie
         * 
         **/
        private void btnEditCategory_Click(object sender, EventArgs e)
        {
            int id = (int)dgvCategories.CurrentRow.Cells["id"].Value;

            categoryForm form = new categoryForm(id);

            DialogResult result = form.ShowDialog();
            if (result == DialogResult.OK)
            {
                this.PopulateAndBindCategories();
            }
        }

        /**
         * 
         * Bouton d'ajout d'une catégorie
         * 
         **/
        private void btnAddCategory_Click(object sender, EventArgs e)
        {
            categoryForm form = new categoryForm();

            DialogResult result = form.ShowDialog();
            if (result == DialogResult.OK)
            {
                this.PopulateAndBindCategories();
            }
        }

        /**
         * 
         * Bouton de suppression d'une catégorie
         * 
         **/
        private void btnDeleteCategory_Click(object sender, EventArgs e)
        {
            int id = (int)dgvCategories.CurrentRow.Cells["id"].Value;
            deleteCategory(id);
        }

        /**
         * 
         * Suppression d'une catégorie
         * 
         **/
        private void deleteCategory(int id)
        {
            BU.CategoryManager manager = new BU.CategoryManager();
            manager.delete(id);
            PopulateAndBindCategories();
        }

        /**
         * 
         * Bouton affichant le panel des produits
         * 
         **/
        private void btnShowProducts_Click(object sender, EventArgs e)
        {
            panelCategory.Visible = false;
            panelOrders.Visible = false;
            populateAndBindProducts();
            panelProducts.Visible = true;
        }

        /**
         * 
         * Bouton affichant le panel des catégories
         * 
         */
        private void btnShowCategories_Click(object sender, EventArgs e)
        {
            panelProducts.Visible = false;
            panelOrders.Visible = false;
            panelCategory.Visible = true;
        }

        /**
         * 
         * Remplit le panel des produits
         * 
         **/
        private void populateAndBindProducts()
        {
            BU.CategoryManager cm = new BU.CategoryManager();
            List<BU.Category> list = cm.getAllAndProducts();

            cbCategory.DataSource = list;
            cbCategory.DisplayMember = "Name";
            cbCategory.ValueMember = "ID";

            dgvProducts.DataSource = list;
            dgvProducts.DataMember = "Products";
        }

        /**
         * 
         * Bouton d'ajout d'un produit
         * 
         **/
        private void btnAddProduct_Click(object sender, EventArgs e)
        {
            int category_id = (int)cbCategory.SelectedValue;
            productForm form = new productForm(0, category_id);

            DialogResult result = form.ShowDialog();
            if (result == DialogResult.OK)
            {
                this.populateAndBindProducts();
            }
        }

        /**
         * 
         * Bouton d'édition d'un produit
         * 
         **/
        private void btnEditProduct_Click(object sender, EventArgs e)
        {
            int id = (int)dgvProducts.CurrentRow.Cells["id"].Value;

            productForm form = new productForm(id);

            DialogResult result = form.ShowDialog();
            if (result == DialogResult.OK)
            {
                this.populateAndBindProducts();
            }
        }


        /**
         * 
         * Bouton de suppression d'un produit
         * 
         **/
        private void btnDeleteProduct_Click(object sender, EventArgs e)
        {
            int id = (int)dgvProducts.CurrentRow.Cells["id"].Value;
            deleteProduct(id);
        }

        /**
         * 
         * Suppression d'un produit
         * 
         **/
        private void deleteProduct(int id)
        {
            BU.ProductManager manager = new BU.ProductManager();
            manager.delete(id);
            populateAndBindProducts();
        }

        /**
         * 
         * Bouton affichant le panel des commandes
         * 
         */
        private void btnShowOrders_Click(object sender, EventArgs e)
        {
            panelOrders.Visible = true;
            panelProducts.Visible = false;
            panelCategory.Visible = false;
            PopulateAndBindOrders();
        }

        /**
         * 
         * Remplit le panel des commandes
         * 
         **/


        private void PopulateAndBindOrders()
        {

            BU.OrderManager cm = new BU.OrderManager();
            List<BU.Order> list = cm.getAll();

            dgvOrder.DataSource = list;
        }

        /**
        * 
        * Bouton de visualisation d'une commande
        * 
        **/
        private void btnEditOrder_Click(object sender, EventArgs e)
        {
            int id = (int)dgvOrder.CurrentRow.Cells["id"].Value;

            orderForm form = new orderForm(id);

            DialogResult result = form.ShowDialog();
        }

        /**
        * 
        * Suppression d'une commande
        * 
        **/
        private void btnDeleteOrder_Click(object sender, EventArgs e)
        {
            int id = (int)dgvOrder.CurrentRow.Cells["id"].Value;
            BU.OrderManager manager = new BU.OrderManager();
            manager.delete(id);
            PopulateAndBindOrders();
        }

        private void btnOrders_Click(object sender, EventArgs e)
        {
            panelOrders.Visible = true;
            panelProducts.Visible = false;
            panelCategory.Visible = false;
            PopulateAndBindOrders();
        }

        private void btnEditOrder_Click_1(object sender, EventArgs e)
        {
            int id = (int)dgvOrder.CurrentRow.Cells["id"].Value;

            orderForm form = new orderForm(id);

            DialogResult result = form.ShowDialog();
        }

        private void btnDeleteOrder_Click_1(object sender, EventArgs e)
        {
            int id = (int)dgvOrder.CurrentRow.Cells["id"].Value;
            BU.OrderManager manager = new BU.OrderManager();
            manager.delete(id);
            PopulateAndBindOrders();
        }
    }
}
